#!/usr/bin/perl

use strict;

my ($taglist , $optlistold, $paraold, $parafirstval,$List,$indentation,$isindentated);   #these variables are used handdle different type of lists 												including tag list, numberd list, bulleted list nested list etc
my ($line);

$isindentated = 0;      #this variable is used check weather indenation is set for .RS tag
$indentation = 0;       #this variable is used to cheack the value of indentation(can be 0 or some number)with .RS tag if $isindentationis true
$List = 0;
$parafirstval = 0;
$paraold = 0;
$taglist = 0;		### 1 = taglist, 0 = other list types except taglist
$optlistold = 0;        ##  1 = previous list entry was there, 0 = no previous entry 

while ($line = <STDIN>)
{
	
	if($line !~ /^\./ )                #if start is not a macro of the form .XY
	{
     		if($line =~ /[\\fB|\\fI]/) #if the start of the line is not a macro then we have to test for \fB and \fI macros
			{				
				MakeMacro($line);
				print "\n";
				next;
			}
			else               #if it does not contain.
			{
				print $line;
				next;
			}
	}
	$line =~ s/^\.//;
	next
		if ($line =~ /\\"/);
	$line = ParseMacro($line);
}

sub ParseMacro
{
	my ($line) = @_;
	
	my (@words,$retval);
	$retval = '';
	@words =  split(/\s+/,$line);
	
	while($_ = shift (@words))
	{
		if(/^sp$/ || /^ne$/ || /^rt$/|| /^mk$/)
		{
			last;
		}
		if(/^RS$/)
		{
			$List = 1;
			$isindentated = scalar(@words); #this is to check whether that indentation value is given, if it is not given tha means we have to 								 use default indentation, if it is given we need to check for that value
							#if given 1
							#if not given 0
			if($isindentated)
			{
				if($_ = shift (@words))
				{
					$indentation = 1;
					last;
				}
				else
				{
					$indentation = 0;
					last;
				}
			}
			else
			{
				$indentation = 1;
				last;
			}
									
		}
		if(/^IP$/ && $List)
		{
			if(!$optlistold)
			{ 
				$optlistold = 1;
				$taglist = 1;
				if($indentation)
				{
					$retval .= ".Bl -tag -offset indent -compact\n";
				}
				else
				{
					$retval .= ".Bl -tab -offset 0n -compact\n";
				}
				print $retval;
				$words[0] =~ s/\\fB/ Nm /;
				$words[0] =~ s/\\fI/ Ar /;
				$words[0] =~ s/\\fR/ /g;
				
				print ".It ".$words[0]."\n";	
				last;
				
			}
			if($optlistold)
			{
				$words[0] =~ s/\\fB/ Nm /;
				$words[0] =~ s/\\fI/ Ar /;
				$words[0] =~ s/\\fR/ /g;
				print ".It ".$words[0]."\n";
				last;
			}
			
		}
		if(/^TP$/ && $List)
		{
			if(!$optlistold)
			{ 
				$optlistold = 1;
				$taglist = 1;
				if($indentation)
				{
					$retval .= ".Bl -tag -offset indent -compact\n";
				}
				else
				{
					$retval .= ".Bl -tab -offset 0n -compact\n";
				}
				print $retval;
				$retval = <STDIN>;
				
				$retval =~ s/\\fB/ Nm /;
				$retval =~ s/\\fI/ Ar /;
				$retval =~ s/\\fR/ /g;
				
				print ".It ".$retval."\n";	
				last;
				
			}
			if($optlistold)
			{
				$retval = <STDIN>;
				$retval =~ s/\\fB/ Nm /;
				$retval =~ s/\\fI/ Ar /;
				$retval =~ s/\\fR/ /g;
				print ".It ".$retval."\n";
				last;
			}
			
		}
		if(/^RE$/)
		{
			$indentation = 0;
			$optlistold = 0;
			$isindentated = 0;
			
			$optlistold = 0;
			last;
			
		}
		if(/^na$/)
		{
			print ".Bl -tag -offset indent -compact\n";
			$retval = <STDIN>;
			$retval =~ s/\\fB/ Nm /;
			$retval =~ s/\\fI/ Ar /;
			$retval =~ s/\\fR/ /g;
			print ".It ".$retval."\n";
			$retval = <STDIN>;
			$retval = <STDIN>;
			$retval = <STDIN>;
			$retval = <STDIN>;
			print $retval;
			$retval = <STDIN>;
			if($retval != /^.RE$/)
		    {
		    	print $retval;
		    }
		    last;
			
								
		}
		
		if(/^IP$/ && !$List)
		{
			if(!$optlistold && $words[0] =~ /^\\\(bu$/)
			{
				$optlistold = 1;
				#$optlist = 1;
				$retval .= ".Bl -bullet"."\n";
				print $retval;
				print ".It \n";
				last;
			}
			
			if(!$optlistold && $words[0] =~ /^-$/)
			{
				$optlistold = 1;
				#$optlist = 1;
				$retval .= ".Bl -dash \n";
				print $retval;
				print ".It \n";
				last;
			}
			
			if(!$optlistold && $words[0] =~ /^[1-9]\.$/)
			{
				$optlistold = 1;
				#$optlist = 1;
				$retval .= ".Bl -enum \n";
				print $retval;
				print ".It \n";
				last;
				
			}
			if(!$optlistold && $words[0] !~ /[0-9|-|(br]/)
			{
				$optlistold = 1;
				$taglist = 1;
				$retval .= ".Bl -tag \n";
				print $retval;
				print ".It ".$words[0]."\n";	
				last;
			}
			if(!$optlistold)
			{
				$optlistold = 1;
				#$optlist = 1;
				$retval .= ".Bl -item \n";
				print $retval;
				print ".It \n";
				last;
				
			}
			if($optlistold)
			{
				print ".It \n";
				last
			}
			#if($taglist)
			#{
			#	print ".It ".@words."\n";	
			#	last;
			#}
			#if(!$taglist) 
			#{
			#	print ".It \n";
			#	$optlistold = 1;
			#	last;
			#}
		}
	
		#if($optlist)
		#{#enum
			#$optlistold = 1;
		#	$optlist = 0;
			#print ".It\n";
		#	$retval = join(' ',$_,@words);
		#	print $retval."\n";
		#	last;
		#}
		#if($optlist == 4)
		#{#item
		#	$optlistold = 1;
		#	$optlist = 0;
		#	$retval = join(' ',$_,@words);
		#	print $retval."\n";
		#	last;
		#}
		if($optlistold && ! /^IP$/ )
		{
			$optlistold = 0;
			print ".El \n";
		}
		if(/^TP$/)
		{
			$parafirstval = 1;
			if(!$paraold)
			{
				$retval .= ".Bl -tag \n";
				print $retval;
				print ".It ";
				$paraold = 1;
				last;
			}
			if($paraold)
			{
				print ".It ";
				$paraold = 1;
				last;
			}
			
		}
		#text bolding (mdoc : .Nm ntpq) (man : .B ntpq )
		if(/^RS$/)
		{
			$List = 1;
			
		}
		if(/^B$/)
		{
			$retval .= ".Nm ".join(' ',@words)."\n";
			print $retval;
			last;
		}
		#text bolding ()
		if(/\\fB/)
		{
			$retval = $_;
			$retval =~  s/[\\fB|\\fP]//g;
			print ".Nm ".$retval."\n";
			last;
		}
		if(/\\fI/)
		{
			$retval = $_;
			$retval =~ s/[\\fI|\\fP]//g;
			print ".Em ".$retval."\n";
			last;	
		}
		if(/^I$/)
		{
			$retval .= ".Em ".join(' ',@words)."\n";
			print $retval;
			last;	
		}
		if(/^PP$/)
		{
			print ".Pp \n";
			last;
			
		}
		if(/^LP$/)
		{
			print ".Pp \n";
			last;
		}
				
		
			
		
	}
		 			
	
}

sub MakeMacro
{
	my (@words);
	@words =  split(/\s+/,$line);
	print "\n";
	while($_ = shift (@words))
	{
		if(/\\fB/ or /\\fI/)
		{
			
			$_ =~ s/\\fB/\.Nm /;
			$_ =~ s/\\fI/\.Ar /;
			$_ =~ s/\\fR//g;
			
			print $_;
			print"\n";
			next;
		}
		else
		{
			print $_." ";
		}
	}
}
